﻿using System;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.Design;

namespace NetJsWire.Design
{

    public class ScrollableControlDesigner : ParentControlDesigner
    {

        public ScrollableControlDesigner()
        {
        }

        private const int HTHSCROLL = 6;
        private const int HTVSCROLL = 7;

        protected override bool GetHitTest( Point point )
        {
            if ( base.GetHitTest( point ) )
            {
                return true;
            }

            // Check if the user has clicked on the scroll bars and forward the message to
            // the ScrollableControl. (Don't filter out the scrolling.). Keep in mind that scrollbars
            // will be shown only if ScrollableControl.AutoScroll = true
            //
            if ( this.Control is ScrollableControl && ( ( ScrollableControl ) Control ).AutoScroll )
            {
                int hitTestResult = ( int ) Native.SendMessage( this.Control.Handle,
                                                                                                                         Native.Msg.WM_NCHITTEST,
                                                                                                                         IntPtr.Zero,
                                                                                                                        ( IntPtr ) Native.LParam( point.X, point.Y ) );
                if ( hitTestResult == HTHSCROLL || hitTestResult == HTVSCROLL )
                    return true;
            }
            return false;
        }


        protected override void WndProc( ref Message m )
        {
            base.WndProc( ref m );
            if ( m.Msg == ( int ) Native.Msg.WM_HSCROLL || m.Msg == ( int ) Native.Msg.WM_VSCROLL )
                this.DefWndProc( ref m );
        }
    }
}